<!DOCTYPE html>
<html lang="en-us">
  <head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="robots" content="noodp"/>
  <meta name="author" content="Mr.wei">
  
  
  
  <link rel="prev" href="http://blog.mr-wei.cn/2019/shell-%E5%8F%98%E9%87%8F/" />
  <link rel="next" href="http://blog.mr-wei.cn/2019/shell-select/" />
  <link rel="canonical" href="http://blog.mr-wei.cn/2019/shell-case%E7%BB%93%E6%9E%84/" />
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
  <link rel="manifest" href="/site.webmanifest">
  <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="theme-color" content="#ffffff">
  <title>
       
       
           shell case结构 | Mr.wei
       
  </title>
  <meta name="title" content="shell case结构 | Mr.wei">
    
  
  <link rel="stylesheet" href="/font/iconfont.css">
  <link rel="stylesheet" href="/css/main.min.css">


  
  
 

<script type="application/ld+json">
 "@context" : "http://schema.org",
    "@type" : "BlogPosting",
    "mainEntityOfPage": {
         "@type": "WebPage",
         "@id": "http:\/\/blog.mr-wei.cn"
    },
    "articleSection" : "posts",
    "name" : "shell case结构",
    "headline" : "shell case结构",
    "description" : " case 结构 case 表达式 in 模式）命令表1;; ... *）命令表n; esac   表达式是判断条件，通常是一个变量名称。表达式与每一分支前的模式进行比较如果匹配，则执行相应分支右括号后面的命令表 每一分支中的模式可以有多个但必须以\x26rsquo;)\x26lsquo;结尾 命令表可以是一个或多个命令 分支语句的个数没有规定，可以无限增加。每一分支必须以\x26rsquo;;;\x26lsquo;结尾 分支\x26rsquo;*)\x26lsquo;表示在前面所有分支的匹配都不满足时的处理方式，该分支可以省略但必须放在其他分支的后面 esca表示case结构的结束 模式中可以使用\x26rsquo;|\x26lsquo;表示各模式之间是或的关系 模式中可以使用shell的通配符\x26rsquo;*\x26rsquo;,\x26lsquo;?\x26rsquo;,\x26rsquo;[]\x26lsquo;等  示例 #!\/bin\/bash case $1 in dir|path) echo \x26quot;Current path is `pwd`\x26quot;;; date|time) echo \x26quot;Today is `date`\x26quot;;; *) echo \x26quot;Invalid argument !\x26quot; echo \x26quot;Enter argument: dir\/path\/date\/time\x26quot;;; esac  参考资料  Linux命令行与shell脚本编程大全  ",
    "inLanguage" : "en-us",
    "author" : "Mr.wei",
    "creator" : "Mr.wei",
    "publisher": "Mr.wei",
    "accountablePerson" : "Mr.wei",
    "copyrightHolder" : "Mr.wei",
    "copyrightYear" : "2019",
    "datePublished": "2019-09-03 10:43:46 \x2b0800 CST",
    "dateModified" : "2019-09-03 10:43:46 \x2b0800 CST",
    "url" : "http:\/\/blog.mr-wei.cn\/2019\/shell-case%E7%BB%93%E6%9E%84\/",
    "wordCount" : "45",
    "keywords" : [ "shell", "Mr.wei"]
}
</script>

</head>

  


  <body class="">
    <div class="wrapper">
        <nav class="navbar">
    <div class="container">
        <div class="navbar-header header-logo">
        	<a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a>
        </div>
        <div class="menu navbar-right">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>
<nav class="navbar-mobile" id="nav-mobile" style="display: none">
     <div class="container">
        <div class="navbar-header">
            <div>  <a href="javascript:void(0);" class="theme-switch"><i class="iconfont icon-xihuan"></i></a>&nbsp;<a href="http://blog.mr-wei.cn">Mr.wei</a></div>
            <div class="menu-toggle">
                <span></span><span></span><span></span>
            </div>
        </div>
     
          <div class="menu" id="mobile-menu">
                
                
                <a class="menu-item" href="/posts/" title="">Blog</a>
                
                <a class="menu-item" href="http://balance.mr-wei.cn" title="">BalanceCar</a>
                
                <a class="menu-item" href="/categories/" title="">Categories</a>
                
                <a class="menu-item" href="/tags/" title="">Tags</a>
                
                <a class="menu-item" href="/about/" title="About">About</a>
                
        </div>
    </div>
</nav>







    	 <main class="main">
          <div class="container">
      		
<article class="post-warp" itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
        <h1 class="post-title" itemprop="name headline">shell case结构</h1>
        <div class="post-meta">
                Written by <a itemprop="name" href="http://blog.mr-wei.cn" rel="author">Mr.wei</a> with ♥ 
                <span class="post-time">
                on <time datetime=2019-09-03 itemprop="datePublished">September 3, 2019</time>
                </span>
                in
                <i class="iconfont icon-folder"></i>
                <span class="post-category">
                        
                </span>
        </div>
    </header>
    <div class="post-content">
        

        

        
        
     
          
          
          

          
          
          

          

<h2 id="case-结构">case 结构</h2>

<pre><code class="language-shell">case 表达式 in
	模式）命令表1;;
	...
	  *）命令表n;
esac
</code></pre>

<!-- more -->

<ul>
<li>表达式是判断条件，通常是一个变量名称。表达式与每一分支前的模式进行比较如果匹配，则执行相应分支右括号后面的命令表</li>
<li>每一分支中的模式可以有多个但必须以&rsquo;)&lsquo;结尾</li>
<li>命令表可以是一个或多个命令</li>
<li>分支语句的个数没有规定，可以无限增加。每一分支必须以&rsquo;;;&lsquo;结尾</li>
<li>分支&rsquo;*)&lsquo;表示在前面所有分支的匹配都不满足时的处理方式，该分支可以省略但必须放在其他分支的后面</li>
<li>esca表示case结构的结束</li>
<li>模式中可以使用&rsquo;|&lsquo;表示各模式之间是或的关系</li>
<li>模式中可以使用shell的通配符&rsquo;*&rsquo;,&lsquo;?&rsquo;,&rsquo;[]&lsquo;等</li>
</ul>

<!-- more -->

<h3 id="示例">示例</h3>

<pre><code class="language-shell">#!/bin/bash
case $1 in
	dir|path) echo &quot;Current path is `pwd`&quot;;;
	date|time) echo &quot;Today is `date`&quot;;;
	*) echo &quot;Invalid argument !&quot;
	   echo &quot;Enter argument: dir/path/date/time&quot;;;
esac
</code></pre>

<h1 id="参考资料">参考资料</h1>

<ul>
<li><font size=6>Linux命令行与shell脚本编程大全</font></li>
</ul>

    </div>

    <div class="post-copyright">
             
            <p class="copyright-item">
                <span>Author:</span>
                <span>Mr.wei </span>
                </p>
            
           
             
            <p class="copyright-item">
                    <span>Link:</span>
                    <a href=http://blog.mr-wei.cn/2019/shell-case%E7%BB%93%E6%9E%84/>http://blog.mr-wei.cn/2019/shell-case%E7%BB%93%E6%9E%84/</span>
            </p>
            
             
            <p class="copyright-item lincese">
                本文采用<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" #target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可
            </p>
            
    </div>

  
    <div class="post-tags">
        
            <section>
            <i class="iconfont icon-tag"></i>Tag(s): 
            
            <span class="tag"><a href="http://blog.mr-wei.cn/tags/shell/">
                    #shell</a></span>
            
            </section>
        
        <section>
                <a href="javascript:window.history.back();">back</a></span> · 
                <span><a href="http://blog.mr-wei.cn">home</a></span>
        </section>
    </div>

    <div class="post-nav">
        
        <a href="http://blog.mr-wei.cn/2019/shell-%E5%8F%98%E9%87%8F/" class="prev" rel="prev" title="shell 变量"><i class="iconfont icon-left"></i>&nbsp;shell 变量</a>
         
        
        <a href="http://blog.mr-wei.cn/2019/shell-select/" class="next" rel="next" title="shell select">shell select&nbsp;<i class="iconfont icon-right"></i></a>
        
    </div>

    <div class="post-comment">
          
                 
          
    </div>
</article>
          </div>
		   </main>
      <footer class="footer">
    <div class="copyright">
        &copy;
        
        <span itemprop="copyrightYear">2018 - 2019</span>
        
        <span class="with-love">
    	 <i class="iconfont icon-love"></i> 
         </span>
         
            <span class="author" itemprop="copyrightHolder"><a href="http://blog.mr-wei.cn">Mr.wei</a> | </span> 
         

         
            <a href="http://www.miibeian.gov.cn/" target="_blank" rel="external nofollow">冀ICP备 - 18032379号 </a> |
         
		  <span>Powered by <a href="https://gohugo.io/" target="_blank" rel="external nofollow">Hugo</a> & <a href="https://github.com/liuzc/leaveit" target="_blank" rel="external nofollow">LeaveIt</a></span> 
    </div>

	
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [['$','$'], ['\\(','\\)']],
            displayMath: [['$$','$$'], ['\[','\]']],
            processEscapes: true,
            processEnvironments: true,
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
            TeX: {
                equationNumbers: { autoNumber: "AMS" },
                extensions: ["AMSmath.js", "AMSsymbols.js"]
            }
        }
    });
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
	
	

</footer>












    
    
    <script src="/js/vendor_no_gallery.min.js" async=""></script>
    
  



     </div>
  </body>
</html>
